package com.coderush.admin.sys.controller;

import com.coderush.admin.core.common.PageResult;
import com.coderush.admin.core.response.Result;
import com.coderush.admin.sys.model.dto.CompanyAddDto;
import com.coderush.admin.sys.model.dto.CompanyUpdateDto;
import com.coderush.admin.sys.model.vo.CompanyPageVo;
import com.coderush.admin.sys.model.vo.CompanySimpleVo;
import com.coderush.admin.sys.model.vo.CompanyVo;
import com.coderush.admin.sys.service.CompanyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * PackageName com.stellarforce.admin.sys.controller
 *
 * @Author Eason
 * @Version v1.0
 * @ClassName CompanyController
 * @Date 2025/8/17
 * @Description 公司控制器
 */
@RestController
@RequestMapping("/sys/company")
@Validated  // 类级别添加，用于方法参数校验
@Tag(name = "公司管理", description = "公司管理")
public class CompanyController {

    @Resource
    private CompanyService companyService;


    /**
     * 查询公司列表
     * @return
     * @throws Exception
     */
    @Operation(summary = "查询公司列表", description = "查询公司列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/findCompanyList")
    public Result<List<CompanySimpleVo>> findCompanyList() throws Exception {

        var companyVoList = companyService.findCompanyList();

        return Result.buildSuccess(companyVoList);
    }


    /**
     * 根据ID查询
     */
    @Operation(summary = "根据ID查询公司", description = "根据ID查询公司")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/getById/{id}")
    public Result<CompanyVo> getById(@PathVariable Long id) throws Exception {
        var companyVo = companyService.getById(id);
        return Result.buildSuccess(companyVo);
    }

    /**
     * 分页查询公司列表
     * @param companyName 公司名称
     * @return
     * @throws Exception
     */
    @Operation(summary = "分页查询公司列表", description = "分页查询公司列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/findByPage")
    public Result<PageResult<CompanyPageVo>> findByPage(String companyName, Integer pageNumber, Integer pageSize) throws Exception {

        var pageResult = companyService.findByPage(companyName, pageNumber, pageSize);

        return Result.buildSuccess(pageResult);
    }

    /**
     * 修改公司信息
     * @param updateDto
     * @return
     * @throws Exception
     */
    @Operation(summary = "修改公司信息", description = "修改公司信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/update")
    public Result<Boolean> update(@Valid @RequestBody CompanyUpdateDto updateDto) throws Exception {

        boolean flag = companyService.update(updateDto);

        return Result.buildSuccess(flag);
    }

    /**
     * 添加公司
     * @param companyDto
     * @return
     * @throws Exception
     */
    @Operation(summary = "添加公司", description = "添加公司")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/add")
    public Result<Boolean> add(@Valid @RequestBody CompanyAddDto companyDto) throws Exception {

        boolean flag = companyService.add(companyDto);

        return Result.buildSuccess(flag);
    }


















}